﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;

namespace MaxLeaf.Platform.DALImplementation
{
    public class mUser_MainDAL :BaseDAL 
    {

        public override string IgnoreUpdateFields
        {
            get
            {
                return "Password";
            }
        }
        public override string[] DoNotUpdateFields
        {
            get
            {
                return new string[] {  "posted" };
            }
        }
         

        #region IUser 成员

        public int Login(string strUserName, string strHashedPassword)
        {

            string strSql = @"select a.* from  mUser_Main a inner join mCompany b on a.CompanyId=b.CompanyId  
                where a.UserName=@UserName and a.Password=@Password and a.is_delete=0  and b.is_delete=0";
            
             using (DBAccess dbcon =DataFunc.GetDBAccessByDataModelType(typeof( mUser_MainDataModel)) )
             {
                 KeyValuePair<string, IDataParameter[]> kvp = dbcon.CreateCondParam(new Dictionary<string, object>() { 
                    { "UserName", strUserName } 
                    ,{ "Password",strHashedPassword } 
                 });
                 using (DbDataReader rdr = dbcon.GetDataReader(strSql, kvp.Value))
                 {
                     if (rdr.Read())
                     {
                         
                         return rdr.GetInt32( rdr.GetOrdinal(DataFunc.GetKeyField(new mUser_MainDataModel())));
                     }
                     else{
                         return 0;
                     }

                 }
             }
        }

        

        #endregion


        protected override string GetCondForPage(DBAccess dbcon, object model, List<Cond> lstCond)
        {
            string strCond = base.GetCondForPage(dbcon, model, lstCond);
            mUser_MainDataModel tModel = (mUser_MainDataModel)model;
            if (lstCond != null)
            {
                foreach (Cond cond in lstCond)
                {
                    if (cond.PropName.Equals("Func_ExistInDepartment", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(strCond))
                        {
                            strCond += " and ";
                        }
                        strCond += "  dbo.F_ExistInDepartment(userid," + cond.Value + " )= " + (cond.Compare == CompareType.Equal ? "1" : "0");
                        continue;
                    }
                    else if (cond.PropName.Equals("Func_IsCompanyApproUser", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(strCond))
                        {
                            strCond += " and ";
                        }
                        strCond +=string.Format( "  exists(select a.UserID from {0}SCPCategoryApproUser a WHERE a.fbilltype=6 and  a.UserID=mUser_Main.UserId and a.CompanyID ={1} ) "
                            ,GetOrdDatabase()
                            , cond.Value

                            );
                        continue;
                    }
                    
                }
            }
            return strCond;
        }

        public bool ResetPassword(string strUserName, string strHashedOldPassword, string strHashedNewPassword)
        {
            bool blnRt=true;
            string strSql = "select * from  mUser_Main  where UserName=@UserName and Password=@Password";
            KeyValuePair<string, IDataParameter[]> kvp;
            using (DBAccess dbcon = DataFunc.GetDBAccessByDataModelType(typeof(mUser_MainDataModel)))
            {
                if (strHashedOldPassword.Length > 0)
                {
                    kvp = dbcon.CreateCondParam(new Dictionary<string, object>() { 
                    { "UserName", strUserName } 
                    ,{ "Password",strHashedOldPassword } 
                 });
                    using (DbDataReader rdr = dbcon.GetDataReader(strSql, kvp.Value))
                    {
                        if (rdr.Read())
                        {

                            blnRt = true;
                        }
                        else
                        {
                            blnRt = false;
                        }

                    }
                }

                if (blnRt)
                {
                    strSql = "update mUser_Main set Password=@Password where UserName=@UserName ";
                    kvp = dbcon.CreateCondParam(new Dictionary<string, object>() { 
                    { "UserName", strUserName } 
                    ,{ "Password",strHashedNewPassword } 
                 });
                    dbcon.ExecuteSql(strSql, kvp.Value);


                    strSql = "update mCompany set Password=@Password  from mUser_Main a where a.UserName=@UserName and a.companyid=mCompany.companyid and a.webuserid=mCompany.webuserid ";
                    kvp = dbcon.CreateCondParam(new Dictionary<string, object>() { 
                    { "UserName", strUserName } 
                    ,{ "Password",strHashedNewPassword } 
                 });
                    dbcon.ExecuteSql(strSql, kvp.Value);
                }
            }
            return blnRt;
        }
    }
}
